package com.shujing.controller;

import com.shujing.pojo.Article;
import com.shujing.pojo.PageBean;
import com.shujing.pojo.Result;
import com.shujing.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 陈书憬
 * @version 1.0
 * 2024/4/8
 */
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/list")
    public Result<String> list() {
        return Result.success("所有文章数据.....");
    }

    /**
     * 该接口用于新增文章(发布文章)
     *
     * @param article
     * @return
     */
    @PostMapping
    public Result addArticle(@RequestBody @Validated Article article) {
        log.info("新增文章数据为:{}", article);
        articleService.addArticle(article);
        return Result.success();
    }

    /**
     * 该接口用于根据条件查询文章,带分页
     *
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param state
     * @return
     */
    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum, Integer pageSize,
                                          @RequestParam(required = false) Integer categoryId,
                                          @RequestParam(required = false) String state) {
        PageBean<Article> articlePageBean = articleService.list(pageNum, pageSize, categoryId, state);
        return Result.success(articlePageBean);
    }


    /**
     * 该接口用于根据ID获取文章详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result<Article> detail(Integer id) {
        Article article = articleService.detail(id);
        return Result.success(article);
    }

    /**
     * 该接口用于更新文章信息
     *
     * @param article
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Article article) {
        articleService.update(article);
        return Result.success();
    }

    /**
     * 该接口用于根据ID删除文章
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id) {
        articleService.delete(id);
        return Result.success();
    }
}
